package com.easondongh.system.controller;

import com.easondongh.system.common.ActiveUser;
import com.easondongh.system.constants.Constants;
import com.easondongh.system.domain.SysUser;
import com.easondongh.common.enums.ResponseEnum;
import com.easondongh.common.response.ResponseObj;
import com.easondongh.system.service.SysUserService;
import com.easondongh.common.shiro.ShiroUtils;
import com.easondongh.system.utils.ChineseToSpell;
import com.easondongh.system.utils.MD5Utils;
import com.easondongh.system.utils.StringUtils;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.vo.UserDispatchVo;
import com.easondongh.system.vo.UserQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

/**
 * @author EasonDongH
 * @date 2020/5/11 9:24
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SysUserService userService;

    @GetMapping("/getCurrentUser")
    public ResponseObj<SysUser> getCurrentUser(){
        ActiveUser activeUser = (ActiveUser)SecurityUtils.getSubject().getPrincipal();
        return new ResponseObj<SysUser>(activeUser.getUser());
    }

    @PostMapping("/getByPage")
    public ResponseObj<PageResult<SysUser>> getByPage(@RequestBody BasePageQueryVo<UserQueryVo> queryVo) {
        if(queryVo == null) {
            return ResponseObj.QueryFail;
        }
        PageResult<SysUser> result = this.userService.getByPage(queryVo);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, result);
    }

    @PostMapping("/addUser")
    public ResponseObj addUser(@RequestBody SysUser user) {
        if(user == null || StringUtils.isBlank(user.getName())) {
            log.error("新增用户时用户名称必填");
            return ResponseObj.AddFail;
        }
        if(StringUtils.isBlank(user.getLoginName())) {
            user.setLoginName(ChineseToSpell.getPingYin(user.getName()));
        }
        if(StringUtils.isBlank(user.getPassword())) {
            user.setPassword(Constants.DEF_PASSWORD);
        }
        if(user.getHireDate() == null) {
            user.setHireDate(new Date());
        }
        user.setSalt(UUID.randomUUID().toString());
        user.setPassword(MD5Utils.md5(user.getPassword(), user.getSalt(), Constants.MD5_HASH_ITEA_Number));
        user.setType(Constants.COMMON_USER);
        this.userService.save(user);
        return ResponseObj.AddSuccess;
    }

    @PutMapping("/enable/{id}")
    public ResponseObj enable(@PathVariable("id") Integer id) {
        if(id == null) {
            return ResponseObj.UpdateFail;
        }
        SysUser user = new SysUser();
        user.setId(id);
        user.setEnable(Constants.ENABLE);
        this.userService.updateById(user);
        return ResponseObj.UpdateSuccess;
    }

    @PutMapping("/disable/{id}")
    public ResponseObj disable(@PathVariable("id") Integer id) {
        if(id == null) {
            return ResponseObj.UpdateFail;
        }
        SysUser user = new SysUser();
        user.setId(id);
        user.setEnable(Constants.DISABLE);
        this.userService.updateById(user);
        return ResponseObj.UpdateSuccess;
    }

    @PutMapping("/updateById")
    public ResponseObj updateById(@RequestBody SysUser user) {
        if(user == null || StringUtils.isBlank(user.getName())) {
            log.warn("修改用户时id、用户名不能为空");
            return ResponseObj.UpdateFail;
        }
        this.userService.updateById(user);
        return ResponseObj.UpdateSuccess;
    }

    @DeleteMapping("/deleteById")
    public ResponseObj deleteById(Integer id) {
        if(id != null) {
                this.userService.removeById(id);
        }
        return ResponseObj.DeleteSuccess;
    }

    /**
     * 分配角色
     * @return
     */
    @PutMapping("/dispatchRole")
    public ResponseObj dispatchMenuAndPermission(@RequestBody UserDispatchVo dispatchVo) {
        if(dispatchVo == null || dispatchVo.getUid() == null) {
            log.warn("为用户分配角色时，用户id不能为空");
            return ResponseObj.UpdateFail;
        }
        this.userService.dispatchRole(dispatchVo);
        return ResponseObj.UpdateSuccess;
    }

    /**
     * 获取当前用户所有角色
     */
    @GetMapping("/getRoles")
    public ResponseObj<UserDispatchVo> getMenuAndPermission(Integer uid) {
        if(uid == null) {
            log.warn("获取当前用户所有角色时用户id不能为空");
            return ResponseObj.QueryFail;
        }
        UserDispatchVo dispatchVo = new UserDispatchVo();
        dispatchVo.setUid(uid);
        dispatchVo.setRids( this.userService.getRoles(uid));
        return new ResponseObj<UserDispatchVo>(ResponseEnum.QUERY_SUCCESS, dispatchVo);
    }

    /**
     * 获取当前最大排序号
     * @return
     */
    @GetMapping("/getMaxOrderNum")
    public ResponseObj<Integer> getMaxOrderNum() {
        Integer orderNum = this.userService.getMaxOrderNum();
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, orderNum);
    }

    /**
     * 更新头像
     * @return
     */
    @PutMapping("/updateAvatar")
    public ResponseObj updateAvatar(@RequestBody String url){
        if(StringUtils.isBlank(url) || StringUtils.isBlank(url = url.replace("\"",""))){
            log.warn("更新头像时url不能为空！");
            return ResponseObj.UpdateFail;
        }
        SysUser loginUser = ShiroUtils.getCurrentLoginUser();
        this.userService.updateAvatar(loginUser.getId(), url);
        return ResponseObj.UpdateSuccess;
    }
}
